package com.ruoyi.keep.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.score.domain.Score;
import com.ruoyi.score.mapper.ScoreMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.keep.mapper.UserlevelMapper;
import com.ruoyi.keep.domain.Userlevel;
import com.ruoyi.keep.service.IUserlevelService;

/**
 * 用户等级Service业务层处理
 * 
 * @author zqd
 * @date 2024-08-14
 */
@Service
public class UserlevelServiceImpl implements IUserlevelService 
{
    @Autowired
    private UserlevelMapper userlevelMapper;

    @Autowired
    private ScoreMapper scoreMapper;

    /**
     * 查询用户等级
     * 
     * @param id 用户等级主键
     * @return 用户等级
     */
    @Override
    public Userlevel selectUserlevelById(Long id)
    {
        return userlevelMapper.selectUserlevelById(id);
    }

    /**
     * 查询用户等级列表
     * 
     * @param userlevel 用户等级
     * @return 用户等级
     */
    @Override
    public List<Userlevel> selectUserlevelList(Userlevel userlevel)
    {
        return userlevelMapper.selectUserlevelList(userlevel);
    }

    /**
     * 查询用户等级列表根据用户id
     *
     * @param
     * @return 用户等级
     */
    @Override
    public Userlevel selectUserlevelByScore(Long userId)
    {
        QueryWrapper<Score> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Long score = scoreMapper.selectOne(wrapper).getScoreTotal();
        List<Userlevel> list = userlevelMapper.selectUserlevelList(null);
        for (int i = list.size()-1; i >=0 ; i--) {
            if (score>list.get(i).getScore()) {
                return list.get(i);
            }
        }
        return null;
    }

    /**
     * 新增用户等级
     * 
     * @param userlevel 用户等级
     * @return 结果
     */
    @Override
    public int insertUserlevel(Userlevel userlevel)
    {
        return userlevelMapper.insertUserlevel(userlevel);
    }

    /**
     * 修改用户等级
     * 
     * @param userlevel 用户等级
     * @return 结果
     */
    @Override
    public int updateUserlevel(Userlevel userlevel)
    {
        return userlevelMapper.updateUserlevel(userlevel);
    }

    /**
     * 批量删除用户等级
     * 
     * @param ids 需要删除的用户等级主键
     * @return 结果
     */
    @Override
    public int deleteUserlevelByIds(Long[] ids)
    {
        return userlevelMapper.deleteUserlevelByIds(ids);
    }

    /**
     * 删除用户等级信息
     * 
     * @param id 用户等级主键
     * @return 结果
     */
    @Override
    public int deleteUserlevelById(Long id)
    {
        return userlevelMapper.deleteUserlevelById(id);
    }
}
